import axios from 'axios'
import router from '../router'
//下面这句是使用ip解决跨域问题
// axios.defaults.baseURL = 'http://61.164.137.226:8089'

axios.defaults.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
// 携带cookie
// axios.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.withCredentials = true

// // http请求拦截器
// axios.interceptors.request.use(
//     config => {
//         if (localStorage.getItem('token')) {
//             config.headers = {
//                 token: localStorage.getItem('token')
//             }
//         }
//         return config
//     },
//     error => {
//         return Promise.reject(error)
//     }
// )

// // http响应拦截器
axios.interceptors.response.use(
    response => {
        let allCookies = document.cookie 
        console.log(allCookies)
        return response
    },
    error => {
        // console.log(error)
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 返回 401 清除token信息并跳转到登录页面
                    localStorage.removeItem('token')
                    router.replace({ 
                        path: '/index',
                        query: {
                            redirect: router.currentRoute.fullPath
                        }
                    })
            }
        }
        return Promise.reject(error)
    }
)

export default axios